<script setup>
import { ref, onMounted } from 'vue'
import router from '@/router';
import loginApi from '@/api/Login_api.js';
import postApi from '../api/Post_api.js'
import classificationApi from '../api/Classification_api.js'
import {ElMessage} from "element-plus";


// 当前用户信息
const post = ref({
  id: '',
  userId: '',
  typeId: '',
  postTitle: '',
  postContent: '',
  postSummary: '',
  giveCount: '',
  commentCount: '',
  status: '',
  isVisible: 0,
})

const loading = ref(false)
const error = ref('')

const onSubmit= async()=>{
  await postApi.add(post.value);
  ElMessage.success('修改成功');
  await router.push('/MyselfPost')
}
// 获取登录用户基本信息
const typename = ref([])
const fetchLoginInfo = async () => {
  loading.value = true
  const res = await loginApi.loginInfo();
  post.value.userId = res.id;
}

// 通过用户ID获取完整用户信息

onMounted(async () => {
  fetchLoginInfo();
  typename.value = await classificationApi.list();
})
// 更新选中的类型对象

// 格式化日期显示

</script>

<template>
  <el-form :model="post" label-width="auto" style="max-width: 600px">
    <el-form-item label="标签">
      <el-select
          v-model="post.typeId"
          placeholder="请选择"
          style="width: 240px"
      >
        <el-option
            v-for="item in typename"
            :key="item.id"
            :label="item.typeName"
            :value="item.id"
        >
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="帖子标题">
      <el-input v-model="post.postTitle" />
    </el-form-item>
    <el-form-item label="帖子正文">
      <el-input v-model="post.postContent" placeholder="请编写内容" type="textarea"/>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit">提交</el-button>
    </el-form-item>
  </el-form>
</template>